         Ferris             The Hex Files             
                Programmer at work - part 3 by Ferris 

 Welcome again, and before we start I'll just give you the
 solution to the little teaser I posed at the end of the
 previous article. The modified routine should look like this:

          *= $0900
          LDX #$00
          LDA #$03      To change the character
LOOP      STA $0400,X
          INX
          CPX #$0B      To change the number of repeats
          BNE LOOP
          RTS

 Okay, lets have some fun with a loop shall we? One of the
 most common things in demos (and in fact most games too) is
 the scrolling message and what we are going to do is a simple
 one with some limits. As we have seen from the example last
 issue it's possible to put characters on the screen very
 fast, so fast in fact that we can't actually see it happen.
 It's also possible to move the characters around the screen
 using loops. Load up the Turbo Assembler you should have
 ended up with after the second part of this course and start
 it with SYS36864, tap return once to get the cursor to its
 start position and enter this program:

          *= $0900
MAIN      LDX #$00
MOVELOOP  LDA $0401,X
          STA $0400,X
          INX
          CPX #$27
          BNE MOVELOOP
          INC $0427
          JMP MAIN

 Before we run it lets look at what you have typed. The *
 command is as we used before, telling Turbo we want our code
 at $0900 (or 2,304 in decimal). We then have a label called
 "main" which is the start of our main code (hence the name!)
 and that clears the X register again as we have done before.
 The main loop of the program (named with the appropriate
 label again) is new though.

 It reads from screen position $0401 and then puts whatever it
 has read into $0400. Then the X register goes up one and it
 repeats that until X reaches $27 (which is 39 in decimal).
 Why stop at 39? Well, when X is 39 the routine is reading
 from $0427 (the top right hand character of the screen) and
 writing to $0426 (one character to the left) so if we
 actually waited until X became 40 (since the screen is 40
 characters wide this would seem logical) we would have moved
 on to the next line of the screen! The reason for this is
 that we are counting from zero not one. Finally we just play
 with the character at the top right of the screen to make
 something to look at (by constantly INCrementing it to make
 it show every character the C64 has).

 Okay, lets crank it up and watch it go! Assembling is as
 before, press the back arrow key at the top left of the
 keyboard and the cursor disappears. Now press the 3 key to
 assemble and when that's done press the S key. Oh. Now
 something is happening but because machine code is so fast we
 can't see what! We need to slow things down a bit and to do
 that I'll introduce a new friend in the form of one of the
 locations in the VIC chip.

 Location $D012 (53,266 in decimal) is known as the raster
 register. The raster is a line that moves down the C64's
 screen redrawing it fifty times a second and there are over
 three hundred "raster lines" on a standard PAL C64 (like the
 one most of us are using) and it's possible to wait for a
 specific line and do something when you get there. Lets alter
 our example to take advantage of this.

 Hit runstop and restore to stop it running and SYS36864 to
 get back into Turbo. Now move the cursor so that it's to the
 right of the second line (the one that reads LDX #$00) and
 then tap return once to insert a blank line. Add the
 following:

          LDA #$FE
RASTER    CMP $D012
          BNE RASTER

 And re-assemble it as before. The three new lines set the A
 register up with a value of $FE (254 in decimal) and then
 compare that to whatever $D012 contains (in the same way as
 we compared numbers in the previous instalment). If it's not
 the same (in other words if the raster isn't at position $FE)
 then the Branch if Not Equal (BNE) back to RASTER keeps it
 waiting until it is. You should now see loads of characters
 scrolling across the top of the screen very fast but not so
 fast that you can't see what's going on. Don't worry about
 the odd jump, we are only experimenting at this point.
 Believe it or not this is moving fifty times a second!

 We have time to add one more feature this issue I think so
 lets jump back into Turbo again. Using the same inserting
 trick I just showed you alter the routine to read like this:

          *= $0900
          LDY #$00      (this line is new)
MAIN      LDX #$00
          LDA #$FE
RASTER    CMP $D012
          BNE RASTER
MOVELOOP  LDA $0401,X
          STA $0400,X
          INX
          CPX #$27
          BNE MOVELOOP
          LDA $A1FF,Y   this line is new too
          STA $0427     this line used to be INC $0427
          INY           this line is new as well
          JMP MAIN      And crank this up.

 This time you will see words flying across your screen! What
 we are actually doing in these new bits is using the Y
 register as a counter and reading from the C64's memory at
 $A1FF onwards. So where do the words come from? Well, $A1FF
 is actually where the C64 keeps some of its error messages
 and this is what you are seeing!

 Well, I think that's almost enough for another instalment but
 before I go another couple of little challenges for you all.
 At the moment the routine reads from $A1FF but can you change
 it to read from $E460? And can you make it work on the next
 line of the screen (remember to change all of the references
 to the screen). I'll give the answers next issue, but If you
 have any questions about this article or machine code, drop
 me an email and I'll see what I can do!

 The next installment of The Hex Files will be added
 soon.</BODY

